<?php


namespace app\adminbackstage\controller;


use app\adminbackstage\model\AdvertisementPosition;
use think\facade\Request;
use think\facade\Config;

/***  广告位置管理
* Class AdvertisementController
 * @package app\adminfocus\controller
 */
class AdvertisementPositionController
{
    //广告列表
    public function advertisementPositionList(){
        $page = Request::param('page');
        $num = Request::param('num');
        $type = Request::param('type');//广告位置
//        $explain = Request::param('explain');//广告名称
//        $starttime = Request::param('starttime');//广告展示开始时间
//        $endtime = Request::param('endtime');//广告展示结束时间
        $enable = Request::param('enable');
        $position = Request::param('position'); //广告位置 由上至下 1为头图下方第一个以此类推
//        $f_page = Request::param('f_page'); //作用页面 页面名

        $where = [];
//        if(!empty($enable)){
//            if($enable == -1){
//                $enable = 0;
//            }
//            $where[] = ['enable','=',$enable];
//        }
        if(!empty($enable)){
            if($enable == -1){
                $enable = 0;
            }
            $where[] = ['enable','=',$enable];
        }
        if(!empty($type)){
            $where[] = ['type','=',$type];
        }
        if(!empty($position)){
            $where[] = ['position','=',$position];
        }
        if(!empty($f_page)){
            $where[] = ['f_page','=',$f_page];
        }
        if(!empty($explain)){
            $where[] = ['explain','like',"%$explain%"];
        }
        if(!empty($starttime)){
            $starttime = strtotime("starttime");
            $where[] = ['start_time','>=',$starttime];
        }
        if(!empty($endtime)){
            $endtime = strtotime("endtime");
            $where[] = ['end_time','<=',$endtime];
        }
        $advertisementPositionList = AdvertisementPosition::advertisementPositionList($where,$page,$num);
        $listinfo = [];
        foreach ($advertisementPositionList as $item){
//            if($item['sort'] == 101){ //排序101表示清空  排序值不在前端显示
//                $item['sort'] = '';
//            }
//            if(!empty($item['start_time'])){
//                $item['start_time'] = date('Y-m-d H:i',$item['start_time']);
//            }
//            if(!empty($item['end_time'])){
//                $item['end_time'] = date('Y-m-d H:i',$item['end_time']);
//            }
            
            if($item['enable']==1){
                $item['enable_str']= '已启用';
            }
            else{
                $item['enable_str']= '已禁用';
            }
            
//            if($item['type']==1){
//                $item['type_str'] = 'PC端';
//            }
//            elseif($item['type']==2){
//                $item['type_str'] = '手机端';
//            }
//            elseif($item['type']==3){
//                $item['type_str'] = '商家个人中心';
//            }
//            elseif($item['type']==4){
//                $item['type_str'] = '推广';
//            }
            
            if(!empty($item['addtime'])){
                $item['addtime'] = date('Y-m-d H:i',$item['addtime']);
            }
//            if(!empty($item['img'])){
//                $item['img'] =  Config::get('app.pomegranate_url').$item['img'];
//            }
            
            $listinfo[] = $item;
        }

        //分页
        $count = AdvertisementPosition::advertisementPositionCount($where);

        return json(['code' => 1, 'msg' => lang(''), 'data' => $listinfo, 'count' => $count]);
    }
    
    public function advertisementPositionInfo(){
        $id =  Request::param('id');
        $where = [];
        $where[] = ['id','=',$id];
        $advertisementPositionView = AdvertisementPosition::advertisementPositionView($where);
//        if($advertisementView['sort'] == 101){ //排序101表示清空  排序值不在前端显示
//            $advertisementView['sort'] = '';
//        }
//        if(!empty($advertisementView['start_time'])){
//            $advertisementView['start_time'] = date('Y-m-d H:i',$advertisementView['start_time']);
//        }
//        if(!empty($advertisementView['end_time'])){
//            $advertisementView['end_time'] = date('Y-m-d H:i',$advertisementView['end_time']);
//        }
//        
//        if($advertisementView['type']==1){
//            $advertisementView['type_str'] = 'PC端';
//        }
//        elseif($advertisementView['type']==2){
//            $advertisementView['type_str'] = '手机端';
//        }
//        elseif($advertisementView['type']==3){
//            $advertisementView['type_str'] = '商家个人中心';
//        }
//        elseif($advertisementView['type']==4){
//            $advertisementView['type_str'] = '推广';
//        }
//        
        if($advertisementPositionInfoView['enable']==1){
            $advertisementPositionInfoView['enable_str']= '已启用';
        }
        else{
            $advertisementPositionInfoView['enable_str']= '已禁用';
        }
        
        if(!empty($advertisementPositionInfoView['addtime'])){
            $advertisementPositionInfoView['addtime'] = date('Y-m-d H:i',$advertisementPositionInfoView['addtime']);
        }
//        if(!empty($advertisementView['img_str'])){
//            $advertisementView['img_str'] =  Config::get('app.pomegranate_url').$advertisementView['img'];
//        
//        }
        return json(['code' => 1, 'msg' => lang(''), 'data' => $advertisementPositionInfoView, 'count' => '']);
    }

    //banner 添加
    public function advertisementPositionAdd(){
        $type = Request::param('type');//类型  1PC端  2手机端  3商家个人中心
//        $explain = Request::param('explain'); //banner名称  / 说明
//        $img = Request::param('img'); //图片地址
//        $link = Request::param('link'); //跳转地址
//        $sort = Request::param('sort'); //排序
//        $starttime = Request::param('starttime'); //展示开始时间
//        $endtime = Request::param('endtime'); //展示结束时间
        $position = Request::param('position'); //广告位置 由上至下 1为头图下方第一个以此类推
        $name = Request::param('name');
//        $f_page = Request::param('f_page'); //作用页面 页面名

        $data = [
            'type' => $type,
            'name' => $name,
            'position' => $position,
            'addtime' => time(),
        ];
        AdvertisementPosition::advertisementPositionAdd($data);
        return json(['code' => 1, 'msg' => lang(''), 'data' => '', 'count' => '']);
    }

    //banner 修改
    public function advertisementPositionEdit(){
        $id = Request::param('id');
        $type = Request::param('type');//类型  1PC端  2手机端  3商家个人中心
 
        $position = Request::param('position'); //广告位置 由上至下 1为头图下方第一个以此类推
        $name = Request::param('name'); //作用页面 页面名
        
        $data = [];
        if(!empty($type)){
            $data['type'] = $type;
        }
      
        
        if(!empty($position)){
            $data['position'] = $position;
        }

        
        if(!empty($name)){
            $data['name'] = $name;
        }

        AdvertisementPosition::advertisementPositionEdit(['id'=>$id],$data);
        
        return json(['code' => 1, 'msg' => lang(''), 'data' => '', 'count' => '']);
    }

    //广告上线，下线
    public function advertisementPositionDel(){
        $id = Request::param('id');
        $enable = Request::param('enable');
        if($enable == 1){
            AdvertisementPosition::advertisementPositionEdit(['id'=>$id],['enable'=>1]);
        }
        else{
            AdvertisementPosition::advertisementPositionEdit(['id'=>$id],['enable'=>0]);
        }
        
        return json(['code' => 1, 'msg' => lang(''), 'data' => '', 'count' => '']);
    }
}